package net.longjin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.longjin.entity.AdviseInfo;
import net.longjin.mapper.AdviseInfoMapper;
import net.longjin.parms.AdviseInfoParms;
import net.longjin.service.AdviseInfoService;
import net.longjin.utils.StringStrUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * (AdviseInfo)表服务实现类
 *
 * @author 何志鹏
 * @since 2020-11-19 13:43:57
 */
@Service("adviseInfoService")
public class AdviseInfoServiceImpl implements AdviseInfoService {

   @Autowired
    private AdviseInfoMapper adviseInfoMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param adviseId 主键
     * @return 实例对象
     */
    @Override
    public AdviseInfo queryById(String adviseId) {
        return this.adviseInfoMapper.selectById(adviseId);
    }

    /**
     * 查询多条数据
     *
     * @return 对象列表
     */
    @Override
    public IPage<AdviseInfo> queryAllByLimit(AdviseInfoParms adviseInfoParms) {
        Page<AdviseInfo> page = new Page<>(adviseInfoParms.getPageNum(),adviseInfoParms.getPageSize());
        QueryWrapper<AdviseInfo> queryWrapper = buildQueryWrapper(adviseInfoParms);
        return   adviseInfoMapper.selectPage(page, queryWrapper);

    }

    /**
     *
     *
     * @param adviseInfoParms
     * @return
     */
    private QueryWrapper<AdviseInfo> buildQueryWrapper(AdviseInfoParms adviseInfoParms) {
        QueryWrapper<AdviseInfo> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(adviseInfoParms.getUserName())){
            queryWrapper.and(wrapper->wrapper.like("user_name",adviseInfoParms.getUserName()));
        }
        return queryWrapper;
    }

    /**
     * 新增数据
     *
     * @param adviseInfo 实例对象
     * @return 实例对象
     */
    @Override
    public int insert(AdviseInfo adviseInfo) {
        adviseInfo.setAdviseId(StringStrUtils.getUUID());
        return this.adviseInfoMapper.insert(adviseInfo);

    }

    /**
     * 修改数据
     *
     * @param adviseInfo 实例对象
     * @return 实例对象
     */
    @Override
    public int update(AdviseInfo adviseInfo) {
        return  this.adviseInfoMapper.updateById(adviseInfo);
    }

    /**
     * 通过主键删除数据
     *
     * @param adviseId 主键
     * @return 是否成功
     */
    @Override
    public int deleteById(String adviseId) {
        return this.adviseInfoMapper.deleteById(adviseId);
    }

    /**
     * 查询所有吐槽  自定义xml查询
     *
     * @param adviseInfo
     * @return
     */
    @Override
    public List<AdviseInfo> queryAll(AdviseInfo adviseInfo) {
        return adviseInfoMapper.queryAll(adviseInfo);
    }
}